
package com.kanjian.music.activity;

import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.FragmentTabHost;
import android.view.KeyEvent;
import android.view.View;
import android.widget.TabHost.TabSpec;
import android.widget.TabWidget;
import android.widget.TextView;

import com.kanjian.music.R;
import com.kanjian.music.fragment.ChatFragment;
import com.kanjian.music.fragment.LiveListFragment;
import com.kanjian.music.fragment.MineFragment;
import com.kanjian.music.fragment.MoreFragment;
import com.kanjian.music.fragment.MusicFragment;
import com.kanjian.music.fragment.RecommendFragment;
import com.kanjian.music.util.DialogUtil;
import com.kanjian.music.util.NotificationUtil;

/**
 * 首页
 * 
 * @author xuyanjun@kanjian.com
 */
public class MainActivity extends BaseActivity {

    private FragmentTabHost mTabHost;

    private TabWidget mTabWidget;

    private String mTabTitleArray[] = {
            "音乐", "直播", "我的", "更多"
    };

    private Class mFragmentArray[] = {
            MusicFragment.class, LiveListFragment.class, MineFragment.class, MoreFragment.class
//            RecommendFragment.class, ChatFragment.class, MineFragment.class, MoreFragment.class
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_main);
        initUI();
        super.onCreate(savedInstanceState);
        setSwipeBackEnable(false);
    }

    private void initUI() {

        mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
        mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);

        for (int i = 0; i < mTabTitleArray.length; i++) {
            TabSpec tabSpec = mTabHost.newTabSpec(mTabTitleArray[i]).setIndicator(
                    getIndicatorView(i));
            mTabHost.addTab(tabSpec, mFragmentArray[i], null);
        }

        mTabWidget = mTabHost.getTabWidget();
        mTabWidget.setDividerDrawable(null);

        // ImageView iv = (ImageView) findViewById(R.id.img);
        // Bitmap bm =
        // BitmapUtil.fastblur(BitmapFactory.decodeResource(getResources(),
        // R.drawable.image_test), 100);
        // iv.setImageBitmap(bm);
    }

    private View getIndicatorView(int index) {
        View v = getLayoutInflater().inflate(R.layout.view_maintab_indicator, null);
        TextView tv = (TextView) v.findViewById(R.id.maintab_text);
        tv.setText(mTabTitleArray[index]);
        return v;
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            DialogUtil.showDialog(this, "退出确认", "是否退出看见音乐", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    finishWithoutAnimation();
//                    NotificationUtil.clearPlayingMusic();
                    dialog.dismiss();
                }
            }, new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            });
            return true;
        } else {
            return super.onKeyDown(keyCode, event);
        }
    }

    @Override
    public void finish() {
        super.finish();
    }

}
